package com.helehousekeeping.xlrequest.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;

import com.helehousekeeping.xlrequest.app.AppContext;

import java.io.File;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @创建者 Jimven
 * @创建时间 2017/11/21/0021  16:18
 */
public class MyUtils {
    public static void showToast(String content) {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        Toast.makeText(AppContext.getContext(), content, Toast.LENGTH_SHORT).show();
    }

    /**
     * 正则表达式匹配判断
     *
     * @param phone 需要做匹配操作的字符串
     * @return true if matched, else false
     */
    public static boolean isPhone(String phone) {
        Pattern pattern = Pattern.compile("^1[0-9]{10}$");
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    public static boolean isMeetPassword(String password) {
        Pattern pattern = Pattern.compile("[a-zA-Z0-9]+");
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

    public static String createSaveDataDir() {
        try {
            String geekPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Party";
            File path = new File(geekPath);
            if (!path.exists()) {
                path.mkdirs();
            }
            return geekPath;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
        * 将秒数转为时分秒
        * */
    public static String change(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        return h + ":" + d + ":" + s + "";
    }

    /**
     * 将每三个数字加上逗号处理（通常使用金额方面的编辑）
     *
     * @param str 需要处理的字符串
     * @return 处理完之后的字符串
     */
    public static String addComma(String str) {
        DecimalFormat decimalFormat = new DecimalFormat(",###");
        return decimalFormat.format(Double.parseDouble(str));
    }
}
